package main

import (
	"fmt"
	"net/http"
	"code.google.com/p/ode/client/go"
)

func handleHello(w http.ResponseWriter, req *http.Request) {

	name := req.FormValue("name")

	if name == "" {
		fmt.Fprintf(w, "Hi! What's your name? Say: <i>My name is ___.</i>")
	} else {
		fmt.Fprintf(w, "Hello, %s! How can I help you?", name)
	}
}

func handleId(w http.ResponseWriter, req *http.Request) {

	name := req.FormValue("name")

	if name == "" {
		fmt.Fprintf(w, "My name is Odie.  I don't know what your name is.")
	} else {
		fmt.Fprintf(w, "My name is Odie.  You are %s.  I thought you'd already know that...", name)
	}
}

func main () {
	e := "http://localhost:8057/hello"
	id := "http://localhost:8057/id"

	matcher := ode.NewMatcher() 

	matcher.AddPattern("My name is $name", e)
	matcher.AddPattern("Hello", e)
	matcher.AddPattern("Call me $name", e)
	matcher.AddPattern("Nice to meet you", e)
	matcher.AddPattern("What is my name", id)
	matcher.AddPattern("Who are you", id)
	matcher.AddPattern("Who am i", id)
	matcher.AddPattern("What is your name", id)
	
	

	http.HandleFunc("/hello", handleHello)
	http.HandleFunc("/id", handleId)

	go matcher.Connect()

	panic(http.ListenAndServe(":8057", nil))
}
